package com.bjpowernode.springboot.controller;

import com.bjpowernode.springboot.entity.BookBean;
import com.bjpowernode.springboot.entity.UserBean;
import com.bjpowernode.springboot.service.BookService;
import com.bjpowernode.springboot.service.TestService;
import com.bjpowernode.springboot.service.UserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import java.util.List;


/**
 * 测试   Controller层    主要是测试redis和mysql连接是否没有问题
 */
@Controller
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    private TestService testService;


    @Autowired
    private UserService userService;

    @Autowired
    private BookService bookService;


    /**
     * 测试mysql连接是否正常      OK
     * @return
     */
    @RequestMapping(value = "/test-mysql-connect")
    @ResponseBody
    public String mysql_connect() {
        boolean isOk = testService.test_mysql_connect();
        if (isOk) {
            return "mysql connect success";
        }
        return "mysql connect faile";
    }

    /**
     * 测试Redis连接是否正常     OK
     * @return
     */
    @RequestMapping(value = "/test-redis-connect")
    @ResponseBody
    public String redis_connect() {
        boolean isOk = testService.test_redis_connect();
        if (isOk) {
            return "redis connect success";
        }
        return "redis connect faile";
    }

    /**
     * 测试从t_user表中获取用户登陆信息的api是否ok     OK
     * @return
     */
    @RequestMapping("/queryall")
    @ResponseBody
    public ModelAndView queryAllUser() {
        List<UserBean> userBeanList = (List<UserBean>) userService.queryAll();
        ModelAndView mv = new ModelAndView();
        mv.addObject("userList",userBeanList);
        mv.setViewName("show-user");
        return mv;
    }

    /**
     * 测试redis相关的功能
     * @return
     */
    @RequestMapping("/test-redis-add")
    @ResponseBody
    public void testRedis1() {
        UserBean userBean = new UserBean();
        userBean.setUsername("张三");
        userBean.setUserId(1);
        userBean.setPassword("root");
        userService.cacheUserBeanToRedis(userBean);
    }

    @RequestMapping("/test-redis-islogin")
    @ResponseBody
    public boolean testRedis2() {
        return userService.isLogined("李四","root");
    }


    /*+++++++++++++++++++++++++++++++++测试BookMapper接口++++++++++++++++++++++++++++++*/

    /**
     * 测试countAllBooks()接口         OK
     * @return
     */
    @RequestMapping("/book-count")
    @ResponseBody
    public int countAllBook() {
        return bookService.countAllBooks();
    }

    /**
     * 测试queryBookByInterval(int start, int offset)接口         OK
     * @param start
     * @param offset
     * @return
     */
    @RequestMapping("/querybook")
    @ResponseBody
    public List<BookBean> queryBookByInterval(int start, int offset) {
        return bookService.queryBookByInterval(start,offset);
    }
}
